/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年11月23日 16:25:34
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.DashLayout;
import com.pactera.jep.sys.model.DashLayoutConfig;
import com.pactera.jep.sys.service.DashLayoutConfigService;
import com.pactera.jep.sys.service.DashLayoutService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.controller.CrudController;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dashLayoutConfig")
public class DashLayoutConfigRestController extends CrudController<DashLayoutConfig>{
	private static Logger	logger	= LoggerFactory.getLogger(DashLayoutConfigRestController.class);

	@Autowired
	private DashLayoutConfigService	dashLayoutConfigService;

	@Autowired
	private DashLayoutService 		dashLayoutService;

	/**
	 * 为列表页面做数据准备
	 */
	@ApiOperation("为列表页面做数据准备")
	@Override
	protected void prepareQuery(Page<DashLayoutConfig> page, List<Filter> filters, List<Order> orders,HttpServletRequest request) {
		
		dashLayoutConfigService.query(page, filters, orders);
	}
	
	/**
	 * 请求布局的配置信息
	 * @param layoutCode
	 * @param model
	 * @return
	 */
	@ApiOperation("请求布局的配置信息")
	@GetMapping(value = "/getLayoutConfigData/{layoutCode}")
	public Map<String,Object> getLayoutConfigData(@PathVariable String layoutCode) {
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			DashLayoutConfig config = dashLayoutConfigService.get(layoutCode);
			JSONObject json=null;
			if(config!=null){
				json=JSONObject.parseObject(config.getConfig());
				map.put("version", config.getVersion());
			}
			map.put("config", json);
			
			DashLayout dashLayout = dashLayoutService.get(layoutCode);
			if(dashLayout != null){
				map.put("designType", dashLayout.getDesignType());
			}
			map.put(WebCode.SUCCESS,true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			map.put(WebCode.SUCCESS, false);
		}
		return map;
	}
	
	/**
	 * 保存
	 * @return
	 */
	@ApiOperation("保存")
	@PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody DashLayoutConfig dashLayoutConfig) {
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		Boolean isExist = dashLayoutConfigService.exists(dashLayoutConfig.getLayoutCode());
		try 
		{
			if(isExist)
			{
				dashLayoutConfigService.update(dashLayoutConfig);
			}
			else
			{
				dashLayoutConfigService.insert(dashLayoutConfig);
			}
			
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}
	
	/**
	 * 删除
	 * @param layoutCode
	 * @return
	 */
	@ApiOperation("删除")
	@RequestMapping(value = "/remove/{layoutCode}")
	public Map<String, Object> remove(@PathVariable String layoutCode){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			dashLayoutConfigService.deleteByPrimaryKey(layoutCode);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} 
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		
		return tips;
	}
	
}